package logic

import (
	"061/Sgame/constant"
	"061/Sgame/db"
	"061/Sgame/server/common"
	"061/Sgame/server/models"
	"061/Sgame/server/web/model"
	"061/Sgame/utils"
	"log"
	"time"
)

var DefaultAccountLogic = &AccountLogic{}

type AccountLogic struct {
}

func (l AccountLogic) Register(rq *model.RegisterReq) error {
	username := rq.Username
	var user models.User
	err := db.GormDB.Where("username=?", username).First(&user).Error
	if err == nil {
		//查询有数据，err为nil 提示用户已存在
		return common.New(constant.UserExist, "用户已存在")
	}
	user.Mtime = time.Now()
	user.Ctime = time.Now()
	user.Username = rq.Username
	user.Passcode = utils.RandSeq(6)
	user.Passwd = utils.Password(rq.Password, user.Passcode)
	user.Hardware = rq.Hardware
	err = db.GormDB.Create(&user).Error
	if err != nil {
		log.Println("注册插入失败", err)
		return common.New(constant.DBError, "数据库异常")
	}
	return nil
}
